<!doctype html>
<html lang="en-US">
  <head>
    <title>Web Chat: Configurable timestamp grouping</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <!--
      This CDN points to the latest official release of Web Chat. If you need to test against Web Chat's latest bits, please refer to using Web Chat's latest bits:
      https://github.com/microsoft/BotFramework-WebChat#how-to-test-with-web-chats-latest-bits
    -->
    <script crossorigin="anonymous" src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
    <style>
      html,
      body {
        height: 100%;
      }

      body {
        display: flex;
        flex-direction: column;
        height: 100%;
        margin: 0;
      }

      #webchat {
        flex: 1;
      }

      #webchat {
        height: 100%;
        width: 100%;
      }

      #buttonBar {
        display: flex;
        flex-wrap: wrap;
        left: 10px;
        margin: 0;
        position: absolute;
        top: 10px;
      }

      #buttonBar > a {
        border: solid 2px #0063b1;
        color: #0063b1;
        font-family: Calibri, 'Helvetica Neue', Arial, sans-serif;
        font-size: 80%;
        margin: 0 10px 10px 0;
        padding: 5px 8px;
        text-decoration: none;
      }

      #buttonBar > a.selected {
        background-color: #0063b1;
        color: White;
      }
    </style>
  </head>
  <body>
    <div id="webchat" role="main"></div>

    <!--
      We will change the URL params when the user select a different timestamp.
      If this sample is implemented over React, we can change `groupTimestamp` on-the-fly without re-render.
    -->
    <p id="buttonBar">
      <a href="?ts=default">Default grouping</a>
      <a href="?ts=false">Don't show timestamp</a>
      <a href="?ts=0">Don't group</a>
      <a href="?ts=2000">Group by 2 seconds</a>
      <a href="?ts=300000">Group by 5 minutes</a>
    </p>

    <script>
      'use strict';

      const groupTimestamp = new URLSearchParams(window.location.search).get('ts') || 'default';

      document.querySelectorAll('#buttonBar > a').forEach(hyperlink => {
        if (hyperlink.getAttribute('href') === `?ts=${groupTimestamp}`) {
          hyperlink.className = 'selected';
        }
      });

      (async function () {
        // In this demo, we are using Direct Line token from MockBot.
        // Your client code must provide either a secret or a token to talk to your bot.
        // Tokens are more secure. To learn about the differences between secrets and tokens
        // and to understand the risks associated with using secrets, visit https://docs.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-direct-line-3-0-authentication?view=azure-bot-service-4.0

        const res = await fetch(
          'https://hawo-mockbot4-token-app.blueriver-ce85e8f0.westus.azurecontainerapps.io/api/token/directline',
          { method: 'POST' }
        );
        const { token } = await res.json();
        const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
          // The following code is for convenience only. It not required for production.
          if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
            dispatch({ type: 'WEB_CHAT/SEND_MESSAGE', payload: { text: 'timestamp' } });
          }

          return next(action);
        });

        window.WebChat.renderWebChat(
          {
            directLine: window.WebChat.createDirectLine({ token }),

            // You can set groupTimestamp to configure how timestamps are grouped.
            // - Set to `undefined` to use default grouping (5 minutes)
            // - Set to `false` to not to group timestamps, it will show on every bubble
            // - Set to `1000` to hide timestamps if they are less than 1 second apart. This can be adjusted (in milliseconds) to any desired number
            // - Set to `0` to show a timestamp with every message
            styleOptions: {
              groupTimestamp:
                groupTimestamp === 'default' ? undefined : groupTimestamp === 'false' ? false : +groupTimestamp
            },
            store
          },
          document.getElementById('webchat')
        );

        // The following code is for convenience only. It not required for production.
        document.querySelector('#webchat > *').focus();
      })().catch(err => console.error(err));
    </script>
  </body>
</html>
